package com.web.admin.web.base.user;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.GetModel;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.facade.RoleFacade;
import com.web.base.auth.message.AppResponse;
import com.web.base.auth.message.RoleRequest;
import com.web.base.auth.message.RoleResponse;
import com.web.base.auth.message.RoleUserRequest;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Controller
@Http(url = "/base/user")
public class UserController
{
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private RoleFacade roleFacade;
	@Autowired
	private AppFacade appFacade;
	
	private DictParser dictParser = DictParser.getInstance();
	
	public UserController()
	{
	}
	
	@Http(url = "/user_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		UserRequest userRequest = request.getQueryObject(UserRequest.class);
		return View.createBuilder().setView("/base/user/user_list")
				.put("model", userRequest)
				.put("page", userFacade.listPage(userRequest, Const.PAGE_START, Const.PAGE_SIZE))
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/user_list", method = HttpMethod.POST)
	public View listPost(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		int start = request.getPostInt("start", Const.PAGE_START);
		return View.createBuilder().setView("/base/user/user_list_grid")
				.put("page", userFacade.listPage(userRequest, start, Const.PAGE_SIZE))
				.build();
	}
	
	@Http(url = "/user_get", method = HttpMethod.GET)
	public View get(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		UserResponse response = null;
		if (id > 0)
		{
			response = userFacade.get(id);
		}
		return View.createBuilder().setView("/base/user/user_get")
				.put("model", response == null ? new UserResponse() : response)
				.put("map", dictParser.getStringMap())
				.build();
	}
	
	@Http(url = "/user_save", method = HttpMethod.POST)
	public void save(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		if (userRequest.getId() == null)
		{
			ValidationUtil.valid(userRequest, SaveModel.class);
			userFacade.add(userRequest);
		}
		else
		{
			ValidationUtil.valid(userRequest, UpdateModel.class);
			userFacade.update(userRequest);
		}
	}
	
	@Http(url = "/user_refresh", method = HttpMethod.POST)
	public void refresh(HttpRequest request)
	{
		roleFacade.refresh();
	}
	
	@Http(url = "/user_role_get", method = HttpMethod.GET)
	public View getRole(HttpRequest request)
	{
		RoleRequest roleRequest = request.getQueryObject(RoleRequest.class, GetModel.class);
		List<RoleResponse> roleList = roleFacade.listSelect(roleRequest.getId());
		Set<Integer> idSet = roleList.stream().map(RoleResponse::getAppId).collect(Collectors.toSet());
		Map<Integer, AppResponse> appMap = appFacade.getList(idSet);
		Map<Integer, List<RoleResponse>> roleMap = roleList.stream().collect(Collectors.groupingBy(RoleResponse::getAppId));
		Map<String, List<RoleResponse>> roleAppMap = new HashMap<>();
		for (Map.Entry<Integer, List<RoleResponse>> entry : roleMap.entrySet())
		{
			AppResponse app = appMap.get(entry.getKey());
			if (app != null)
			{
				roleAppMap.put(app.getName(), entry.getValue());
			}
		}
		return View.createBuilder().setView("/base/user/user_role_get")
				.put("model", userFacade.get(roleRequest.getId()))
				.put("roleMap", roleAppMap)
				.build();
	}
	
	@Http(url = "/user_role_save", method = HttpMethod.POST)
	public void saveMenu(HttpRequest request)
	{
		RoleUserRequest roleUserRequest = request.getPostObject(RoleUserRequest.class, SaveModel.class);
		roleFacade.addRoleUser(roleUserRequest);
	}
	
}
